home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / includes / locale.inc < prev    next >
Encoding:
Text File  |  2005-04-02  |  46.8 KB  |  1,312 lines

  1. <?php
  2. // $Id: locale.inc,v 1.42 2005/04/02 18:22:17 unconed Exp $
  3.  
  4. /**
  5.  * @file
  6.  * Admin-related functions for locale.module.
  7.  */
  8.  
  9. // ---------------------------------------------------------------------------------
  10. // Language addition functionality (administration only)
  11.  
  12. /**
  13.  * Helper function to add a language
  14.  */
  15. function _locale_add_language($code, $name, $onlylanguage = TRUE) {
  16.   db_query("INSERT INTO {locales_meta} (locale, name) VALUES ('%s','%s')", $code, $name);
  17.   $result = db_query("SELECT lid FROM {locales_source}");
  18.   while ($string = db_fetch_object($result)) {
  19.     db_query("INSERT INTO {locales_target} (lid, locale) VALUES (%d,'%s')", $string->lid, $code);
  20.   }
  21.  
  22.   // If only the language was added, and not a PO file import triggered
  23.   // the language addition, we need to inform the user on how to start
  24.   // a translation
  25.   if ($onlylanguage) {
  26.     $message = t('%locale language added. You can now import a translation. See the <a href="%locale-help">help screen</a> for more information.', array('%locale' => theme('placeholder', t($name)), '%locale-help' => url('admin/help/locale')));
  27.   }
  28.   else {
  29.     $message = t('%locale language added.', array('%locale' => theme('placeholder', t($name))));
  30.   }
  31.  
  32.   drupal_set_message($message);
  33.   watchdog('locale', t('%language language (%locale) added.', array('%language' => theme('placeholder', $name), '%locale' => theme('placeholder', $code))));
  34. }
  35.  
  36. /**
  37.  * User interface for the language management screen
  38.  */
  39. function _locale_admin_manage_screen() {
  40.   $edit = &$_POST['edit'];
  41.   $languages = locale_supported_languages(TRUE, TRUE);
  42.  
  43.   $header = array(array('data' => t('Code')), array('data' => t('English name')), array('data' => t('Enabled')), array('data' => t('Default')), array('data' => t('Translated')), array('data' => t('Operations')));
  44.  
  45.   foreach ($languages['name'] as $key => $lang) {
  46.  
  47.     $status = db_fetch_object(db_query("SELECT isdefault, enabled FROM {locales_meta} WHERE locale = '%s'", $key));
  48.  
  49.     if ($key == 'en') {
  50.       $rows[] = array('en', check_plain($lang), form_checkbox('', 'enabled][en', 1, $status->enabled), form_radio('', 'sitedefault', $key, $status->isdefault), message_na(), '');
  51.     }
  52.     else {
  53.       $original = db_fetch_object(db_query("SELECT COUNT(*) AS strings FROM {locales_source}"));
  54.       $translation = db_fetch_object(db_query("SELECT COUNT(*) AS translation FROM {locales_target} WHERE locale = '%s' AND translation != ''", $key));
  55.  
  56.       $ratio = ($original->strings > 0 && $translation->translation > 0) ? round(($translation->translation/$original->strings)*100., 2) : 0;
  57.  
  58.       $rows[] = array(check_plain($key), ($key != 'en' ? form_textfield('', 'name]['. $key, $lang, 15, 64) : $lang), form_checkbox('', 'enabled]['. $key, 1, $status->enabled), form_radio('', 'sitedefault', $key, $status->isdefault), "$translation->translation/$original->strings ($ratio%)", ($key != 'en' ? l(t('delete'), 'admin/locale/language/delete/'. urlencode($key)) : ''));
  59.     }
  60.   }
  61.  
  62.   return form(theme('table', $header, $rows) . form_submit(t('Save configuration')), 'POST', url('admin/locale'));
  63. }
  64.  
  65. /**
  66.  * User interface for the language addition screen
  67.  */
  68. function _locale_admin_manage_add_screen() {
  69.  
  70.   $isocodes = _locale_prepare_iso_list();
  71.  
  72.   $output = '<h2>'. t('From language list') .'</h2>';
  73.   $form = form_select(t('Language name'), 'langcode', key($isocodes), $isocodes, t('Select your language here, or add it below, if you are unable to find it.'));
  74.   $form .= form_submit(t('Add language'));
  75.   $output .= form($form);
  76.  
  77.   $edit = &$_POST['edit'];
  78.   $output .= '<h2>'. t('Custom language') .'</h2>';
  79.   $form = form_textfield(t('Language code'), 'langcode', $edit['langcode'], 70, 12, t("Commonly this is an <a href=\"%iso-codes\">ISO 639 language code</a> with an optional country code for regional variants. Examples include 'en', 'en-US' and 'zh-cn'.", array('%iso-codes' => 'http://www.w3.org/WAI/ER/IG/ert/iso639.htm')));
  80.   $form .= form_textfield(t('Language name in English'), 'langname', $edit['langname'], 70, 64, t('Name of the language. Will be available for translation in all languages.'));
  81.   $form .= form_submit(t('Add language'));
  82.   $output .= form($form);
  83.  
  84.   return $output;
  85. }
  86.  
  87.  
  88. /**
  89.  * User interface for the translation import screen
  90.  */
  91. function _locale_admin_import_screen() {
  92.   $languages = locale_supported_languages(FALSE, TRUE);
  93.   $languages = array_map("t", $languages['name']);
  94.   unset($languages['en']);
  95.  
  96.   if (!count($languages)) {
  97.     $languages = _locale_prepare_iso_list();
  98.   }
  99.   else {
  100.     $languages = array(
  101.       t('Already added languages') => $languages,
  102.       t('Languages not yet added') => _locale_prepare_iso_list()
  103.     );
  104.   }
  105.  
  106.   $form = form_file(t('Language file'), 'file', 50, t('A gettext Portable Object (.po) file.'));
  107.   $form .= form_select(t('Import into'), 'langcode', '', $languages, t('Choose the language you want to add strings into. If you choose a language which is not yet set up, then it will be added.'));
  108.   $form .= form_radios(t('Mode'), 'mode', 'overwrite', array('overwrite' => t('Strings in the uploaded file replace existing ones, new ones are added'), 'keep' => t('Existing strings are kept, only new strings are added')));
  109.   $form .= form_submit(t('Import'));
  110.   $output = form($form, 'POST', url('admin/locale/language/import'), array('enctype' => 'multipart/form-data'));
  111.   return $output;
  112. }
  113.  
  114. /**
  115.  * Parses Gettext Portable Object file information and inserts into database
  116.  *
  117.  * @param $file Object contains properties of local file to be imported
  118.  * @param $edit Language code
  119.  * @param $mode should existing translations be replaced?
  120.  */
  121. function _locale_import_po($file, $lang, $mode) {
  122.   // If not in 'safe mode', increase the maximum execution time:
  123.   if (!ini_get('safe_mode')) {
  124.     set_time_limit(240);
  125.   }
  126.  
  127.   // Check if we have the language already in the database
  128.   if (!db_fetch_object(db_query("SELECT locale FROM {locales_meta} WHERE locale = '%s'", $lang))) {
  129.     drupal_set_message(t('Unsupported language selected for import.'), 'error');
  130.     return FALSE;
  131.   }
  132.  
  133.   // Check if we can get the strings from the file
  134.   if (!($strings = _locale_import_read_po($file))) {
  135.     drupal_set_message(t('Translation file %filename broken: Could not be read.', array('%filename' => theme('placeholder', $file->filename))), 'error');
  136.     return FALSE;
  137.   }
  138.  
  139.   // Strip out header from the string pairs
  140.   $header = $strings[""]["msgstr"];
  141.   unset($strings[""]);
  142.  
  143.   // Get information from the header into the database
  144.   if ($header) {
  145.     $hdr = _locale_import_parse_header($header);
  146.  
  147.     // Get the plural formula
  148.     if ($hdr["Plural-Forms"] && $p = _locale_import_parse_plural_forms($hdr["Plural-Forms"], $file->filename)) {
  149.       list($nplurals, $plural) = $p;
  150.       db_query("UPDATE {locales_meta} SET plurals = '%d', formula = '%s' WHERE locale = '%s'", $nplurals, $plural, $lang);
  151.     }
  152.     else {
  153.       db_query("UPDATE {locales_meta} SET plurals = '%d', formula = '%s' WHERE locale = '%s'", 0, '', $lang);
  154.     }
  155.   }
  156.   else {
  157.     drupal_set_message(t('Translation file %filename broken: No header.', array('%filename' => theme('placeholder', $file->filename))), 'error');
  158.     return FALSE;
  159.   }
  160.  
  161.   $additions = 0;
  162.   $updates = 0;
  163.   foreach ($strings as $value) {
  164.     $comments = _locale_import_shorten_comments($value['#']);
  165.  
  166.     // Handle a translation for some plural string
  167.     if (strpos($value['msgid'], "\0")) {
  168.       $english = explode("\0", $value['msgid'], 2);
  169.       $entries = array_keys($value['msgstr']);
  170.       for ($i = 3; $i <= count($entries); $i++) {
  171.         $english[] = $english[1];
  172.       }
  173.       $translation = array_map('_locale_import_append_plural', $value['msgstr'], $entries);
  174.       $english = array_map('_locale_import_append_plural', $english, $entries);
  175.       foreach ($translation as $key => $trans) {
  176.         if ($key == 0) {
  177.           $plid = 0;
  178.         }
  179.         $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english[$key]));
  180.         if ($loc->lid) { // a string exists
  181.           $lid = $loc->lid;
  182.           // update location field
  183.           db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $comments, $lid);
  184.           $trans2 = db_fetch_object(db_query("SELECT lid, translation, plid, plural FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $lang));
  185.           if (!$trans2->lid) { // no translation in current language
  186.             db_query("INSERT INTO {locales_target} (lid, locale, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, $trans, $plid, $key);
  187.             $additions++;
  188.           } // translation exists
  189.           else if ($mode == 'overwrite' || $trans2->translation == '') {
  190.             db_query("UPDATE {locales_target} SET translation = '%s', plid = %d, plural = %d WHERE locale = '%s' AND lid = %d", $trans, $plid, $key, $lang, $lid);
  191.             if ($trans2->translation == '') {
  192.               $additions++;
  193.             }
  194.             else {
  195.               $updates++;
  196.             }
  197.           }
  198.         }
  199.         else { // no string
  200.           db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", $comments, $english[$key]);
  201.           $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english[$key]));
  202.           $lid = $loc->lid;
  203.           db_query("INSERT INTO {locales_target} (lid, locale, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $lang, $trans, $plid, $key);
  204.           if ($trans != '') {
  205.             $additions++;
  206.           }
  207.         }
  208.         $plid = $lid;
  209.       }
  210.     }
  211.  
  212.     // A simple translation
  213.     else {
  214.       $english = $value['msgid'];
  215.       $translation = $value['msgstr'];
  216.       $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english));
  217.       if ($loc->lid) { // a string exists
  218.         $lid = $loc->lid;
  219.         // update location field
  220.         db_query("UPDATE {locales_source} SET location = '%s' WHERE source = '%s'", $comments, $english);
  221.         $trans = db_fetch_object(db_query("SELECT lid, translation FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $lang));
  222.         if (!$trans->lid) { // no translation in current language
  223.           db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '%s')", $lid, $lang, $translation);
  224.           $additions++;
  225.         } // translation exists
  226.         else if ($mode == 'overwrite') { //overwrite in any case
  227.           db_query("UPDATE {locales_target} SET translation = '%s' WHERE locale = '%s' AND lid = %d", $translation, $lang, $lid);
  228.           if ($trans->translation == '') {
  229.             $additions++;
  230.           }
  231.           else {
  232.             $updates++;
  233.           }
  234.         } // overwrite if empty string
  235.         else if ($trans->translation == '') {
  236.           db_query("UPDATE {locales_target} SET translation = '%s' WHERE locale = '%s' AND lid = %d", $translation, $lang, $lid);
  237.           $additions++;
  238.         }
  239.       }
  240.       else { // no string
  241.         db_query("INSERT INTO {locales_source} (location, source) VALUES ('%s', '%s')", $comments, $english);
  242.         $loc = db_fetch_object(db_query("SELECT lid FROM {locales_source} WHERE source = '%s'", $english));
  243.         $lid = $loc->lid;
  244.         db_query("INSERT INTO {locales_target} (lid, locale, translation) VALUES (%d, '%s', '%s')", $lid, $lang, $translation);
  245.         if ($translation != '') {
  246.           $additions++;
  247.         }
  248.       }
  249.     }
  250.   }
  251.  
  252.   // Successful import
  253.   // rebuild locale cache
  254.   cache_clear_all("locale:$lang");
  255.  
  256.   // rebuild the menu, strings may have changed
  257.   menu_rebuild();
  258.  
  259.   drupal_set_message(t('Translation successfully imported. %number translated strings added to language, %update strings updated.', array('%number' => $additions, '%update' => $updates)));
  260.   watchdog('locale', t('Imported %file into %locale: %number new strings added and %update updated.', array('%file' => theme('placeholder', $file->filename), '%locale' => theme('placeholder', $lang), '%number' => $additions, '%update' => $updates)));
  261.   return TRUE;
  262. }
  263.  
  264. /**
  265.  * Parses Gettext Portable Object file into an array
  266.  *
  267.  * @param $file Object with properties of local file to parse
  268.  * @author Jacobo Tarrio
  269.  */
  270. function _locale_import_read_po($file) {
  271.  
  272.   $message = theme('placeholder', $file->filename);
  273.   $fd = fopen($file->filepath, "rb");
  274.   if (!$fd) {
  275.     drupal_set_message(t('Translation import failed: file %filename cannot be read.', array('%filename' => $message)), 'error');
  276.     return FALSE;
  277.   }
  278.   $info = fstat($fd);
  279.   $len = $info["size"];
  280.   $po = fread($fd, $len);
  281.   fclose($fd);
  282.  
  283.   $context = "COMMENT"; // Parser context: COMMENT, MSGID, MSGID_PLURAL, MSGSTR and MSGSTR_ARR
  284.   $current = array();   // Current entry being read
  285.   $strings = array();   // List of entries read
  286.   $plural = 0;          // Current plural form
  287.  
  288.   $po = strtr($po, array("\\\n" => ""));
  289.   $lines = split("\n", $po);
  290.   $lineno = 0;
  291.  
  292.   foreach ($lines as $line) {
  293.     $lineno++;
  294.     $line = trim($line);
  295.  
  296.     if (!strncmp("#", $line, 1)) { // A comment
  297.       if ($context == "COMMENT") { // Already in comment context: add
  298.         $current["#"][] = substr($line, 1);
  299.       }
  300.       elseif (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) { // End current entry, start a new one
  301.         $strings[$current["msgid"]] = $current;
  302.         $current = array();
  303.         $current["#"][] = substr($line, 1);
  304.         $context = "COMMENT";
  305.       }
  306.       else { // Parse error
  307.         drupal_set_message(t("Translation file %filename broken: expected 'msgstr' in line %line.", array('%filename' => $message, '%line' => $lineno)), 'error');
  308.         return FALSE;
  309.       }
  310.     }
  311.     elseif (!strncmp("msgid_plural", $line, 12)) {
  312.       if ($context != "MSGID") { // Must be plural form for current entry
  313.         drupal_set_message(t("Translation file %filename broken: unexpected 'msgid_plural' in line %line.", array('%filename' => $message, '%line' => $lineno)), 'error');
  314.         return FALSE;
  315.       }
  316.       $line = trim(substr($line, 12));
  317.       $quoted = _locale_import_parse_quoted($line);
  318.       if ($quoted === false) {
  319.         drupal_set_message(t('Translation file %filename broken: syntax error in line %line.', array('%filename' => $message, '%line' => $lineno)), 'error');
  320.         return FALSE;
  321.       }
  322.       $current["msgid"] = $current["msgid"] ."\0". $quoted;
  323.       $context = "MSGID_PLURAL";
  324.     }
  325.     elseif (!strncmp("msgid", $line, 5)) {
  326.       if ($context == "MSGSTR") {   // End current entry, start a new one
  327.         $strings[$current["msgid"]] = $current;
  328.         $current = array();
  329.       }
  330.       elseif ($context == "MSGID") { // Already in this context? Parse error
  331.         drupal_set_message(t("Translation file %filename broken: unexpected 'msgid' in line %line.", array('%filename' => $message, '%line' => $lineno)), 'error');
  332.         return FALSE;
  333.       }
  334.       $line = trim(substr($line, 5));
  335.       $quoted = _locale_import_parse_quoted($line);
  336.       if ($quoted === false) {
  337.         drupal_set_message(t('Translation file %filename broken: syntax error in line %line.', array('%filename' => $message, '%line' => $lineno)), 'error');
  338.         return FALSE;
  339.       }
  340.       $current["msgid"] = $quoted;
  341.       $context = "MSGID";
  342.     }
  343.     elseif (!strncmp("msgstr[", $line, 7)) {
  344.       if (($context != "MSGID") && ($context != "MSGID_PLURAL") && ($context != "MSGSTR_ARR")) { // Must come after msgid, msgid_plural, or msgstr[]
  345.         drupal_set_message(t("Translation file %filename broken: unexpected 'msgstr[]' in line %line.", array('%filename' => $message, '%line' => $lineno)), 'error');
  346.         return FALSE;
  347.       }
  348.       if (strpos($line, "]") === false) {
  349.         drupal_set_message(t('Translation file %filename broken: syntax error in line %line.', array('%filename' => $message, '%line' => $lineno)), 'error');
  350.         return FALSE;
  351.       }
  352.       $frombracket = strstr($line, "[");
  353.       $plural = substr($frombracket, 1, strpos($frombracket, "]") - 1);
  354.       $line = trim(strstr($line, " "));
  355.       $quoted = _locale_import_parse_quoted($line);
  356.       if ($quoted === false) {
  357.         drupal_set_message(t('Translation file %filename broken: syntax error in line %line.', array('%filename' => $message, '%line' => $lineno)), 'error');
  358.         return FALSE;
  359.       }
  360.       $current["msgstr"][$plural] = $quoted;
  361.       $context = "MSGSTR_ARR";
  362.     }
  363.     elseif (!strncmp("msgstr", $line, 6)) {
  364.       if ($context != "MSGID") {   // Should come just after a msgid block
  365.         drupal_set_message(t("Translation file %filename broken: unexpected 'msgstr' in line %line.", array('%filename' => $message, '%line' => $lineno)), 'error');
  366.         return FALSE;
  367.       }
  368.       $line = trim(substr($line, 6));
  369.       $quoted = _locale_import_parse_quoted($line);
  370.       if ($quoted === false) {
  371.         drupal_set_message(t('Translation file %filename broken: syntax error in line %line.', array('%filename' => $message, '%line' => $lineno)), 'error');
  372.         return FALSE;
  373.       }
  374.       $current["msgstr"] = $quoted;
  375.       $context = "MSGSTR";
  376.     }
  377.     elseif ($line != "") {
  378.       $quoted = _locale_import_parse_quoted($line);
  379.       if ($quoted === false) {
  380.         drupal_set_message(t('Translation file %filename broken: syntax error in line %line.', array('%filename' => $message, '%line' => $lineno)), 'error');
  381.         return FALSE;
  382.       }
  383.       if (($context == "MSGID") || ($context == "MSGID_PLURAL")) {
  384.         $current["msgid"] .= $quoted;
  385.       }
  386.       elseif ($context == "MSGSTR") {
  387.         $current["msgstr"] .= $quoted;
  388.       }
  389.       elseif ($context == "MSGSTR_ARR") {
  390.         $current["msgstr"][$plural] .= $quoted;
  391.       }
  392.       else {
  393.         drupal_set_message(t('Translation file %filename broken: unexpected string in line %line.', array('%filename' => $message, '%line' => $lineno)), 'error');
  394.         return FALSE;
  395.       }
  396.     }
  397.   }
  398.  
  399.   // End of PO file, flush last entry
  400.   if (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) {
  401.     $strings[$current["msgid"]] = $current;
  402.   }
  403.   elseif ($context != "COMMENT") {
  404.     drupal_set_message(t('Translation file %filename broken: unexpected end of file at line %line.', array('%filename' => $message, '%line' => $lineno)), 'error');
  405.     return FALSE;
  406.   }
  407.  
  408.   return $strings;
  409. }
  410.  
  411. /**
  412.  * Parses a Gettext Portable Object file header
  413.  *
  414.  * @param $header A string containing the complete header
  415.  * @return An associative array of key-value pairs
  416.  * @author Jacobo Tarrio
  417.  */
  418. function _locale_import_parse_header($header) {
  419.   $hdr = array();
  420.  
  421.   $lines = explode("\n", $header);
  422.   foreach ($lines as $line) {
  423.     $line = trim($line);
  424.     if ($line) {
  425.       list($tag, $contents) = explode(":", $line, 2);
  426.       $hdr[trim($tag)] = trim($contents);
  427.     }
  428.   }
  429.  
  430.   return $hdr;
  431. }
  432.  
  433. /**
  434.  * Parses a Plural-Forms entry from a Gettext Portable Object file header
  435.  *
  436.  * @param $pluralforms A string containing the Plural-Forms entry
  437.  * @param $filename A string containing the filename
  438.  * @return An array containing the number of plurals and a
  439.  *         formula in PHP for computing the plural form
  440.  * @author Jacobo Tarrio
  441.  */
  442. function _locale_import_parse_plural_forms($pluralforms, $filename) {
  443.   // First, delete all whitespace
  444.   $pluralforms = strtr($pluralforms, array(" " => "", "\t" => ""));
  445.  
  446.   // Select the parts that define nplurals and plural
  447.   $nplurals = strstr($pluralforms, "nplurals=");
  448.   if (strpos($nplurals, ";")) {
  449.     $nplurals = substr($nplurals, 9, strpos($nplurals, ";") - 9);
  450.   }
  451.   else {
  452.     return FALSE;
  453.   }
  454.   $plural = strstr($pluralforms, "plural=");
  455.   if (strpos($plural, ";")) {
  456.     $plural = substr($plural, 7, strpos($plural, ";") - 7);
  457.   }
  458.   else {
  459.     return FALSE;
  460.   }
  461.  
  462.   // Get PHP version of the plural formula
  463.   $plural = _locale_import_parse_arithmetic($plural);
  464.  
  465.   if ($plural) {
  466.     return array($nplurals, $plural);
  467.   }
  468.   else {
  469.     drupal_set_message(t("Translation file %filename broken: plural formula couldn't get parsed.", array('%filename' => theme('placeholder', $filename))), 'error');
  470.     return FALSE;
  471.   }
  472. }
  473.  
  474. /**
  475.  * Parses and sanitizes an arithmetic formula into a PHP expression
  476.  *
  477.  * While parsing, we ensure, that the operators have the right
  478.  * precedence and associativity.
  479.  *
  480.  * @param $string A string containing the arithmetic formula
  481.  * @return The PHP version of the formula
  482.  * @author Jacobo Tarrio
  483.  */
  484. function _locale_import_parse_arithmetic($string) {
  485.   // Operator precedence table
  486.   $prec = array("(" => -1, ")" => -1, "?" => 1, ":" => 1, "||" => 3, "&&" => 4, "==" => 5, "!=" => 5, "<" => 6, ">" => 6, "<=" => 6, ">=" => 6, "+" => 7, "-" => 7, "*" => 8, "/" => 8, "%" => 8);
  487.   // Right associativity
  488.   $rasc = array("?" => 1, ":" => 1);
  489.  
  490.   $tokens = _locale_import_tokenize_formula($string);
  491.  
  492.   // Parse by converting into infix notation then back into postfix
  493.   $opstk = array();
  494.   $elstk = array();
  495.  
  496.   foreach ($tokens as $token) {
  497.     $ctok = $token;
  498.  
  499.     // Numbers and the $n variable are simply pushed into $elarr
  500.     if (is_numeric($token)) {
  501.       $elstk[] = $ctok;
  502.     }
  503.     elseif ($ctok == "n") {
  504.       $elstk[] = '$n';
  505.     }
  506.     elseif ($ctok == "(") {
  507.       $opstk[] = $ctok;
  508.     }
  509.     elseif ($ctok == ")") {
  510.       $topop = array_pop($opstk);
  511.       while (($topop != NULL) && ($topop != "(")) {
  512.         $elstk[] = $topop;
  513.         $topop = array_pop($opstk);
  514.       }
  515.     }
  516.     elseif ($prec[$ctok]) {
  517.       // If it's an operator, then pop from $oparr into $elarr until the
  518.       // precedence in $oparr is less than current, then push into $oparr
  519.       $topop = array_pop($opstk);
  520.       while (($topop != NULL) && ($prec[$topop] >= $prec[$ctok]) && !(($prec[$topop] == $prec[$ctok]) && $rasc[$topop] && $rasc[$ctok])) {
  521.         $elstk[] = $topop;
  522.         $topop = array_pop($opstk);
  523.       }
  524.       if ($topop) {
  525.         $opstk[] = $topop;   // Return element to top
  526.       }
  527.       $opstk[] = $ctok;      // Parentheses are not needed
  528.     }
  529.     else {
  530.       return false;
  531.     }
  532.   }
  533.  
  534.   // Flush operator stack
  535.   $topop = array_pop($opstk);
  536.   while ($topop != NULL) {
  537.     $elstk[] = $topop;
  538.     $topop = array_pop($opstk);
  539.   }
  540.  
  541.   // Now extract formula from stack
  542.   $prevsize = count($elstk) + 1;
  543.   while (count($elstk) < $prevsize) {
  544.     $prevsize = count($elstk);
  545.     for ($i = 2; $i < count($elstk); $i++) {
  546.       $op = $elstk[$i];
  547.       if ($prec[$op]) {
  548.         $f = "";
  549.         if ($op == ":") {
  550.           $f = $elstk[$i - 2] ."):". $elstk[$i - 1] .")";
  551.         }
  552.         elseif ($op == "?") {
  553.           $f = "(". $elstk[$i - 2] ."?(". $elstk[$i - 1];
  554.         }
  555.         else {
  556.           $f = "(". $elstk[$i - 2] . $op . $elstk[$i - 1] .")";
  557.         }
  558.         array_splice($elstk, $i - 2, 3, $f);
  559.         break;
  560.       }
  561.     }
  562.   }
  563.  
  564.   // If only one element is left, the number of operators is appropriate
  565.   if (count($elstk) == 1) {
  566.     return $elstk[0];
  567.   }
  568.   else {
  569.     return FALSE;
  570.   }
  571. }
  572.  
  573. /**
  574.  * Backward compatible implementation of token_get_all() for formula parsing
  575.  *
  576.  * @param $string A string containing the arithmetic formula
  577.  * @return The PHP version of the formula
  578.  * @author Gerhard Killesreiter
  579.  */
  580. function _locale_import_tokenize_formula($formula) {
  581.   $formula = str_replace(" ", "", $formula);
  582.   $tokens = array();
  583.   for ($i = 0; $i < strlen($formula); $i++) {
  584.     if (is_numeric($formula{$i})) {
  585.       $num = $formula{$i};
  586.       $j = $i + 1;
  587.       while($j < strlen($formula) && is_numeric($formula{$j})) {
  588.         $num .= $formula{$j};
  589.         $j++;
  590.       }
  591.       $i = $j - 1;
  592.       $tokens[] = $num;
  593.     }
  594.     elseif ($pos = strpos(" =<>!&|", $formula{$i})) { // We won't have a space
  595.       $next = $formula{($i+1)};
  596.       switch ($pos) {
  597.         case 1:
  598.         case 2:
  599.         case 3:
  600.         case 4:
  601.           if ($next == '=') {
  602.             $tokens[] = $formula{$i} .'=';
  603.             $i++;
  604.           }
  605.           else {
  606.             $tokens[] = $formula{$i};
  607.           }
  608.           break;
  609.         case 5:
  610.           if ($next == '&') {
  611.             $tokens[] = '&&';
  612.             $i++;
  613.           }
  614.           else {
  615.             $tokens[] = $formula{$i};
  616.           }
  617.           break;
  618.         case 6:
  619.           if ($next == '|') {
  620.             $tokens[] = '||';
  621.             $i++;
  622.           }
  623.           else {
  624.             $tokens[] = $formula{$i};
  625.           }
  626.           break;
  627.       }
  628.     }
  629.     else {
  630.       $tokens[] = $formula{$i};
  631.     }
  632.   }
  633.   return $tokens;
  634. }
  635.  
  636. /**
  637.  * Modify a string to contain proper count indices
  638.  *
  639.  * This is a callback function used via array_map()
  640.  *
  641.  * @param $entry An array element
  642.  * @param $key Index of the array element
  643.  */
  644. function _locale_import_append_plural($entry, $key) {
  645.   // No modifications for 0, 1
  646.   if ($key == 0 || $key == 1) {
  647.     return $entry;
  648.   }
  649.  
  650.   // First remove any possibly false indices, then add new ones
  651.   $entry = preg_replace('/(%count)\[[0-9]\]/', '\\1', $entry);
  652.   return preg_replace('/(%count)/', "\\1[$key]", $entry);
  653. }
  654.  
  655. /**
  656.  * Generate a short, one string version of the passed comment array
  657.  *
  658.  * @param $comment An array of strings containing a comment
  659.  * @return Short one string version of the comment
  660.  */
  661. function _locale_import_shorten_comments($comment) {
  662.   $comm = '';
  663.   while(strlen($comm) < 128 && count($comment)) {
  664.     $comm .= substr(array_shift($comment), 1) .', ';
  665.   }
  666.   return substr($comm, 0, -2);
  667. }
  668.  
  669. /**
  670.  * Parses a string in quotes
  671.  *
  672.  * @param $string A string specified with enclosing quotes
  673.  * @return The string parsed from inside the quotes
  674.  */
  675. function _locale_import_parse_quoted($string) {
  676.   if (substr($string, 0, 1) != substr($string, -1, 1)) {
  677.     return FALSE;   // Start and end quotes must be the same
  678.   }
  679.   $quote = substr($string, 0, 1);
  680.   $string = substr($string, 1, -1);
  681.   if ($quote == '"') {        // Double quotes: strip slashes
  682.     return stripcslashes($string);
  683.   }
  684.   elseif ($quote == "'") {  // Simple quote: return as-is
  685.     return $string;
  686.   }
  687.   else {
  688.     return FALSE;             // Unrecognized quote
  689.   }
  690. }
  691.  
  692. /**
  693.  * User interface for the translation export screen
  694.  */
  695. function _locale_admin_export_screen() {
  696.   $languages = locale_supported_languages(FALSE, TRUE);
  697.   $languages = array_map("t", $languages['name']);
  698.   unset($languages['en']);
  699.   $output = '';
  700.  
  701.   // Offer language specific export if any language is set up
  702.   if (count($languages)) {
  703.     $output .= '<h2>'. t('Export translation') .'</h2>';
  704.     $form = form_select(t('Language name'), 'langcode', '', $languages, t('Select the language you would like to export in gettext Portable Object (.po) format.'));
  705.     $form .= form_submit(t('Export'));
  706.     $output .= form($form);
  707.   }
  708.  
  709.   // Complete template export of the strings
  710.   $output .= '<h2>'. t('Export template') .'</h2>';
  711.   $form = t('<p>Generate a gettext Portable Object Template (.pot) file with all the interface strings from the Drupal locale database.</p>');
  712.   $form .= form_submit(t('Export'));
  713.   $output .= form($form);
  714.  
  715.   return $output;
  716. }
  717.  
  718. /**
  719.  * Exports a Portable Object (Template) file for a language
  720.  *
  721.  * @param $language Selects a language to generate the output for
  722.  */
  723. function _locale_export_po($language) {
  724.   global $user;
  725.  
  726.   // Get language specific strings, or all strings
  727.   if ($language) {
  728.     $meta = db_fetch_object(db_query("SELECT * FROM {locales_meta} WHERE locale = '%s'", $language));
  729.     $result = db_query("SELECT s.lid, s.source, s.location, t.translation, t.plid, t.plural FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.locale = '%s' ORDER BY t.plid, t.plural", $language);
  730.   }
  731.   else {
  732.     $result = db_query("SELECT s.lid, s.source, s.location, t.plid, t.plural FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid GROUP BY s.lid ORDER BY t.plid, t.plural");
  733.   }
  734.  
  735.   // Build array out of the database results
  736.   $parent = array();
  737.   while ($child = db_fetch_object($result)) {
  738.     if ($child->source != '') {
  739.       $parent[$child->lid]['comment'] = $child->location;
  740.       $parent[$child->lid]['msgid'] = $child->source;
  741.       if ($child->plid) {
  742.         $parent[$child->lid][$child->plid]['plural'] = $child->lid;
  743.         $parent[$child->lid][$child->plid]['translation'] = $child->translation;
  744.         $parent[$child->lid][$child->plid]['msgid'] = $child->source;
  745.       }
  746.       else {
  747.         $parent[$child->lid]['translation'] = $child->translation;
  748.       }
  749.     }
  750.   }
  751.  
  752.   // Generating Portable Object file for a language
  753.   if ($language) {
  754.     $filename = $language .'.po';
  755.     $header .= "# $meta->name translation of ". variable_get('site_name', 'Drupal') ."\n";
  756.     $header .= '# Copyright (c) '. date('Y') .' '. $user->name .' <'. $user->mail .">\n";
  757.     $header .= "#\n";
  758.     $header .= "msgid \"\"\n";
  759.     $header .= "msgstr \"\"\n";
  760.     $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
  761.     $header .= "\"POT-Creation-Date: ". date("Y-m-d H:iO") ."\\n\"\n";
  762.     $header .= "\"PO-Revision-Date: ". date("Y-m-d H:iO") ."\\n\"\n";
  763.     $header .= "\"Last-Translator: ". $user->name .' <'. $user->mail .">\\n\"\n";
  764.     $header .= "\"Language-Team: ". $meta->name .' <'. $user->mail .">\\n\"\n";
  765.     $header .= "\"MIME-Version: 1.0\\n\"\n";
  766.     $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  767.     $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
  768.     if ($meta->formula && $meta->plurals) {
  769.       $header .= "\"Plural-Forms: nplurals=". $meta->plurals ."; plural=". strtr($meta->formula, '$', '') .";\\n\"\n";
  770.     }
  771.     $header .= "\n";
  772.     watchdog('locale', t('Exported %locale translation file: %filename.', array('%locale' => theme('placeholder', $meta->name), '%filename' => theme('placeholder', $filename))));
  773.   }
  774.  
  775.   // Generating Portable Object Template
  776.   else {
  777.     $filename = variable_get('site_name', 'drupal') .'.pot';
  778.     $header .= "# LANGUAGE translation of PROJECT\n";
  779.     $header .= "# Copyright (c) YEAR NAME <EMAIL@ADDRESS>\n";
  780.     $header .= "#\n";
  781.     $header .= "msgid \"\"\n";
  782.     $header .= "msgstr \"\"\n";
  783.     $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
  784.     $header .= "\"POT-Creation-Date: ". date("Y-m-d H:iO") ."\\n\"\n";
  785.     $header .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
  786.     $header .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
  787.     $header .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
  788.     $header .= "\"MIME-Version: 1.0\\n\"\n";
  789.     $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  790.     $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
  791.     $header .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n";
  792.     $header .= "\n";
  793.     watchdog('locale', t('Exported translation file: %filename.', array('%filename' => theme('placeholder', $filename))));
  794.   }
  795.  
  796.   // Start download process
  797.   header("Content-Disposition: attachment; filename=$filename");
  798.   header("Content-Type: text/plain; charset=utf-8");
  799.  
  800.   print $header;
  801.  
  802.   foreach ($parent as $lid => $message) {
  803.     if (!isset($done[$lid])) {
  804.       if ($message['comment']) {
  805.         print '#: '. $message['comment'] ."\n";
  806.       }
  807.       print 'msgid '. _locale_export_print($message['msgid']);
  808.       if (isset($message[1]['plural'])) {
  809.         print 'msgid_plural '. _locale_export_print($message[1]['msgid']);
  810.         if ($language) {
  811.           for ($i = 0; $i < $meta->plurals; $i++) {
  812.             print 'msgstr['. $i .'] '. _locale_export_print(_locale_export_remove_plural($message[${i}]['translation']));
  813.             $done[$message[${i}]['plural']] = 1;
  814.           }
  815.         }
  816.         else {
  817.           print 'msgstr[0] ""'. "\n";
  818.           print 'msgstr[1] ""'. "\n";
  819.           $done[$message[0]['plural']] = 1;
  820.           $done[$message[1]['plural']] = 1;
  821.         }
  822.       }
  823.       else {
  824.         if ($language) {
  825.           print 'msgstr '. _locale_export_print($message['translation']);
  826.         }
  827.         else {
  828.           print 'msgstr ""'. "\n";
  829.         }
  830.       }
  831.       print "\n";
  832.     }
  833.   }
  834.   die();
  835. }
  836.  
  837. /**
  838.  * Print out a string on multiple lines
  839.  */
  840. function _locale_export_print($str) {
  841.   $stri = addcslashes($str, "\0..\37\\\"");
  842.   $parts = array();
  843.  
  844.   // Cut text into several lines
  845.   while ($stri != "") {
  846.     $i = strpos($stri, "\\n");
  847.     if ($i === FALSE) {
  848.       $curstr = $stri;
  849.       $stri = "";
  850.     }
  851.     else {
  852.       $curstr = substr($stri, 0, $i + 2);
  853.       $stri = substr($stri, $i + 2);
  854.     }
  855.     $curparts = explode("\n", _locale_export_wrap($curstr, 70));
  856.     $parts = array_merge($parts, $curparts);
  857.   }
  858.  
  859.   if (count($parts) > 1) {
  860.     return "\"\"\n\"". implode("\"\n\"", $parts) ."\"\n";
  861.   }
  862.   else {
  863.     return "\"$parts[0]\"\n";
  864.   }
  865. }
  866.  
  867. /**
  868.  * Custom word wrapping for Portable Object (Template) files.
  869.  *
  870.  * @author Jacobo Tarrio
  871.  */
  872. function _locale_export_wrap($str, $len) {
  873.   $words = split(" ", $str);
  874.   $ret = array();
  875.  
  876.   $cur = "";
  877.   $nstr = 1;
  878.   while (count($words)) {
  879.     $word = array_shift($words);
  880.     if ($nstr) {
  881.       $cur = $word;
  882.       $nstr = 0;
  883.     }
  884.     elseif (strlen("$cur $word") > $len) {
  885.       $ret[] = $cur . " ";
  886.       $cur = $word;
  887.     }
  888.     else {
  889.       $cur = "$cur $word";
  890.     }
  891.   }
  892.   $ret[] = $cur;
  893.  
  894.   return implode("\n", $ret);
  895. }
  896.  
  897. /**
  898.  * Removes plural index information from a string
  899.  */
  900. function _locale_export_remove_plural($entry) {
  901.   return preg_replace('/(%count)\[[0-9]\]/', '\\1', $entry);
  902. }
  903.  
  904. function _locale_string_delete($lid) {
  905.   db_query('DELETE FROM {locales_source} WHERE lid = %d', $lid);
  906.   db_query('DELETE FROM {locales_target} WHERE lid = %d', $lid);
  907.   locale_refresh_cache();
  908.   drupal_set_message(t('Deleted string'));
  909. }
  910.  
  911. /**
  912.  * Action handler for string editing
  913.  *
  914.  * Saves all translations of one string submitted from a form
  915.  */
  916. function _locale_string_save($lid) {
  917.  
  918.   $edit =& $_POST['edit'];
  919.   foreach ($edit as $key => $value) {
  920.     $trans = db_fetch_object(db_query("SELECT translation FROM {locales_target} WHERE lid = %d AND locale = '%s'", $lid, $key));
  921.     if (isset($trans->translation)) {
  922.       db_query("UPDATE {locales_target} SET translation = '%s' WHERE lid = %d AND locale = '%s'", $value, $lid, $key);
  923.     }
  924.     else {
  925.       db_query("INSERT INTO {locales_target}  (lid, translation, locale) VALUES (%d, '%s', '%s')", $lid, $value, $key);
  926.     }
  927.   }
  928.   // refresh the locale cache
  929.   locale_refresh_cache();
  930.   // rebuild the menu, strings may have changed
  931.   menu_rebuild();
  932.   // delete form data so it will remember where it came from
  933.   $edit = '';
  934.  
  935.   drupal_set_message(t('Saved string'));
  936. }
  937.  
  938. /**
  939.  * User interface for string editing
  940.  */
  941. function _locale_string_edit($lid) {
  942.   $languages = locale_supported_languages(FALSE, TRUE);
  943.   unset($languages['name']['en']);
  944.  
  945.   $result = db_query('SELECT DISTINCT s.source, t.translation, t.locale FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE s.lid = %d', $lid);
  946.   $form = '';
  947.   while ($translation = db_fetch_object($result)) {
  948.     $orig = $translation->source;
  949.     $form .= (strlen($orig) > 40) ? form_textarea($languages['name'][$translation->locale], $translation->locale, $translation->translation, 70, 15) : form_textfield($languages['name'][$translation->locale], $translation->locale, $translation->translation, 50, 128);
  950.     unset($languages['name'][$translation->locale]);
  951.   }
  952.   foreach ($languages['name'] as $key => $lang) {
  953.     $form .= (strlen($orig) > 40) ? form_textarea($lang, $key, '', 70, 15) : form_textfield($lang, $key, '', 50, 128);
  954.   }
  955.   $form = form_item(t('Original text'), wordwrap(check_plain($orig, 0))) . $form;
  956.  
  957.   $form .= form_submit(t('Save translations'));
  958.  
  959.   return form($form);
  960. }
  961.  
  962. /**
  963.  * List languages in search result table
  964.  */
  965. function _locale_string_language_list($translation) {
  966.   $languages = locale_supported_languages(FALSE, TRUE);
  967.   unset($languages['name']['en']);
  968.   $output = '';
  969.   foreach ($languages['name'] as $key => $value) {
  970.     if (isset($translation[$key])) {
  971.       $output .= ($translation[$key] != '') ? $key .' ' : "<strike>$key</strike> ";
  972.     }
  973.   }
  974.  
  975.   return $output;
  976. }
  977.  
  978. /**
  979.  * Build object out of search criteria specified in request variables
  980.  */
  981. function _locale_string_seek_query() {
  982.   static $query = NULL;
  983.  
  984.   if (is_null($query)) {
  985.     $fields = array('string', 'language', 'searchin');
  986.     $query = new StdClass;
  987.     if (is_array($_REQUEST['edit'])) {
  988.       foreach ($_REQUEST['edit'] as $key => $value) {
  989.         if (!empty($value) && in_array($key, $fields)) {
  990.           $query->$key = $value;
  991.         }
  992.       }
  993.     }
  994.     else {
  995.       foreach ($_REQUEST as $key => $value) {
  996.         if (!empty($value) && in_array($key, $fields)) {
  997.           $query->$key = strpos(',', $value) ? explode(',', $value) : $value;
  998.         }
  999.       }
  1000.     }
  1001.   }
  1002.   return $query;
  1003. }
  1004.  
  1005. /**
  1006.  * Perform a string search and display results in a table
  1007.  */
  1008. function _locale_string_seek() {
  1009.   // We have at least one criterion to match
  1010.   if ($query = _locale_string_seek_query()) {
  1011.     $join = "SELECT s.source, s.location, s.lid, t.translation, t.locale FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid ";
  1012.  
  1013.     // Compute LIKE section
  1014.     switch ($query->searchin) {
  1015.       case 'translated':
  1016.         $where = "WHERE (t.translation LIKE '%". db_escape_string($query->string) ."%' AND t.translation != '')";
  1017.         $orderby = "ORDER BY t.translation";
  1018.         break;
  1019.       case 'untranslated':
  1020.         $where = "WHERE (s.source LIKE '%". db_escape_string($query->string) ."%' AND t.translation = '')";
  1021.         $orderby = "ORDER BY s.source";
  1022.         break;
  1023.       case 'all' :
  1024.       default:
  1025.         $where = "WHERE (s.source LIKE '%". db_escape_string($query->string) ."%' OR t.translation LIKE '%". db_escape_string($query->string) ."%')";
  1026.         $orderby = '';
  1027.         break;
  1028.     }
  1029.  
  1030.     switch ($query->language) {
  1031.       // Force search in source strings
  1032.       case "en":
  1033.         $sql = $join ." WHERE s.source LIKE '%". db_escape_string($query->string) ."%' ORDER BY s.source";
  1034.         break;
  1035.       // Search in all languages
  1036.       case "all":
  1037.         $sql = "$join $where $orderby";
  1038.         break;
  1039.       // Some different language
  1040.       default:
  1041.         $sql = "$join $where AND t.locale = '". db_escape_string($query->language) ."' $orderby";
  1042.     }
  1043.  
  1044.     $result = pager_query($sql, 50);
  1045.  
  1046.     $header = array(t('String'), t('Locales'), array('data' => t('Operations'), 'colspan' => '2'));
  1047.     $arr = array();
  1048.     while ($locale = db_fetch_object($result)) {
  1049.       $arr[$locale->lid]['locales'][$locale->locale] = $locale->translation;
  1050.       $arr[$locale->lid]['location'] = $locale->location;
  1051.       $arr[$locale->lid]['source'] = $locale->source;
  1052.     }
  1053.     foreach ($arr as $lid => $value) {
  1054.       $source = htmlspecialchars($value['source']);
  1055.       $rows[] = array(array('data' => (strlen($source) > 150 ? substr($source, 0, 150) .'...' : $source) .'<br /><small>'. $value['location'] .'</small>'), array('data' => _locale_string_language_list($value['locales']), 'align' => 'center'), array('data' => l(t('edit'), "admin/locale/string/edit/$lid"), 'nowrap' => 'nowrap'), array('data' => l(t('delete'), "admin/locale/string/delete/$lid"), 'nowrap' => 'nowrap'));
  1056.     }
  1057.  
  1058.     $request = array();
  1059.     if (count($query)) {
  1060.       foreach ($query as $key => $value) {
  1061.         $request[$key] = (is_array($value)) ? implode(',', $value) : $value;
  1062.       }
  1063.     }
  1064.  
  1065.     if ($pager = theme('pager', NULL, 50, 0, $request)) {
  1066.       $rows[] = array(array('data' => "$pager", 'colspan' => '5'));
  1067.     }
  1068.  
  1069.     $output .= theme('table', $header, $rows);
  1070.  
  1071.   }
  1072.  
  1073.   return $output;
  1074. }
  1075.  
  1076. /**
  1077.  * User interface for the string search screen
  1078.  */
  1079. function _locale_string_seek_form() {
  1080.  
  1081.   // Get *all* languages set up
  1082.   $languages = locale_supported_languages(FALSE, TRUE);
  1083.   asort($languages['name']); unset($languages['name']['en']);
  1084.   $languages['name'] = array_map('check_plain', $languages['name']);
  1085.  
  1086.   // Present edit form preserving previous user settings
  1087.   $query = _locale_string_seek_query();
  1088.   $form .= form_textfield(t('Strings to search for'), 'string', $query->string, 30, 30, t('Leave blank to show all strings. The search is case sensitive.'));
  1089.   $form .= form_radios(t('Language'), 'language', ($query->language ? $query->language : 'all'), array_merge(array('all' => t('All languages'), 'en' => t('English (provided by Drupal)')), $languages['name']));
  1090.   $form .= form_radios(t('Search in'), 'searchin', ($query->searchin ? $query->searchin : 'all'), array('all' => t('All strings in that language'), 'translated' => t('Only translated strings'), 'untranslated' => t('Only untranslated strings')));
  1091.  
  1092.   $form .= form_submit(t('Search'));
  1093.   $output = form(form_group(t('Search strings'), $form), 'POST', url('admin/locale/string/search'));
  1094.  
  1095.   return $output;
  1096. }
  1097.  
  1098. // ---------------------------------------------------------------------------------
  1099. // List of some of the most common languages (administration only)
  1100.  
  1101. /**
  1102.  * Prepares the language code list for a select form item with only the unsupported ones
  1103.  */
  1104. function _locale_prepare_iso_list() {
  1105.   $languages = locale_supported_languages(FALSE, TRUE);
  1106.   $isocodes = _locale_get_iso639_list();
  1107.   foreach ($isocodes as $key => $value) {
  1108.     if (isset($languages['name'][$key])) {
  1109.       unset($isocodes[$key]);
  1110.       continue;
  1111.     }
  1112.     if (count($value) == 2) {
  1113.       $tname = t($value[0]);
  1114.       $isocodes[$key] = ($tname == $value[1]) ? $tname : "$tname ($value[1])";
  1115.     }
  1116.     else {
  1117.       $isocodes[$key] = t($value[0]);
  1118.     }
  1119.   }
  1120.   asort($isocodes);
  1121.   return $isocodes;
  1122. }
  1123.  
  1124. /**
  1125.  * Some of the common languages with their English and native names
  1126.  *
  1127.  * Based on ISO 639 and http://people.w3.org/rishida/names/languages.html
  1128.  */
  1129. function _locale_get_iso639_list() {
  1130.   return array(
  1131.     "aa" => array("Afar"),
  1132.     "ab" => array("Abkhazian", "╨░╥º╤ü╤â╨░ ╨▒╤ï╨╖╤ê╙Ö╨░"),
  1133.     "ae" => array("Avestan"),
  1134.     "af" => array("Afrikaans"),
  1135.     "ak" => array("Akan"),
  1136.     "am" => array("Amharic", "ßèáßê¢ßê¡ßè¢"),
  1137.     "ar" => array("Arabic", "╪º┘ä╪╣╪▒╪¿┘è╪⌐"),
  1138.     "as" => array("Assamese"),
  1139.     "av" => array("Avar"),
  1140.     "ay" => array("Aymara"),
  1141.     "az" => array("Azerbaijani", "az╔Örbaycan"),
  1142.     "ba" => array("Bashkir"),
  1143.     "be" => array("Belarusian", "╨æ╨╡╨╗╨░╤Ç╤â╤ü╨║╨░╤Å"),
  1144.     "bg" => array("Bulgarian", "╨æ╤è╨╗╨│╨░╤Ç╤ü╨║╨╕"),
  1145.     "bh" => array("Bihari"),
  1146.     "bi" => array("Bislama"),
  1147.     "bm" => array("Bambara", "Bamanankan"),
  1148.     "bn" => array("Bengali"),
  1149.     "bo" => array("Tibetan"),
  1150.     "br" => array("Breton"),
  1151.     "bs" => array("Bosnian", "Bosanski"),
  1152.     "ca" => array("Catalan", "Catal├á"),
  1153.     "ce" => array("Chechen"),
  1154.     "ch" => array("Chamorro"),
  1155.     "co" => array("Corsican"),
  1156.     "cr" => array("Cree"),
  1157.     "cs" => array("Czech", "─îe┼ítina"),
  1158.     "cu" => array("Old Slavonic"),
  1159.     "cv" => array("Welsh", "Cymraeg"),
  1160.     "cy" => array("Welch"),
  1161.     "da" => array("Danish"),
  1162.     "de" => array("German", "Deutsch"),
  1163.     "dv" => array("Maldivian"),
  1164.     "dz" => array("Bhutani"),
  1165.     "ee" => array("Ewe", "╞É╩ï╔¢"),
  1166.     "el" => array("Greek", "╬ò╬╗╬╗╬╖╬╜╬╣╬║╬¼"),
  1167.     "en" => array("English"),
  1168.     "eo" => array("Esperanto"),
  1169.     "es" => array("Spanish", "Espa├▒ol"),
  1170.     "et" => array("Estonian", "Eesti"),
  1171.     "eu" => array("Basque", "Euskera"),
  1172.     "fa" => array("Persian", "┘ü╪º╪▒╪│█î"),
  1173.     "ff" => array("Fulah", "Fulfulde"),
  1174.     "fi" => array("Finnish", "Suomi"),
  1175.     "fj" => array("Fiji"),
  1176.     "fo" => array("Faeroese"),
  1177.     "fr" => array("French", "Fran├ºais"),
  1178.     "fy" => array("Frisian", "Frysk"),
  1179.     "ga" => array("Irish", "Gaeilge"),
  1180.     "gd" => array("Scots Gaelic"),
  1181.     "gl" => array("Galician", "Galego"),
  1182.     "gn" => array("Guarani"),
  1183.     "gu" => array("Gujarati"),
  1184.     "gv" => array("Manx"),
  1185.     "ha" => array("Hausa"),
  1186.     "he" => array("Hebrew", "╫ó╫æ╫¿╫Ö╫¬"),
  1187.     "hi" => array("Hindi", "αñ╣αñ┐αñ¿αÑìαñªαÑÇ"),
  1188.     "ho" => array("Hiri Motu"),
  1189.     "hr" => array("Croatian", "Hrvatski"),
  1190.     "hu" => array("Hungarian", "Magyar"),
  1191.     "hy" => array("Armenian", "╒Ç╒í╒╡╒Ñ╓Ç╒Ñ╒╢"),
  1192.     "hz" => array("Herero"),
  1193.     "ia" => array("Interlingua"),
  1194.     "id" => array("Indonesian", "Bahasa Indonesia"),
  1195.     "ie" => array("Interlingue"),
  1196.     "ig" => array("Igbo"),
  1197.     "ik" => array("Inupiak"),
  1198.     "is" => array("Icelandic", "├ìslenska"),
  1199.     "it" => array("Italian", "Italiano"),
  1200.     "iu" => array("Inuktitut"),
  1201.     "ja" => array("Japanese", "µùѵ£¼Φ¬₧"),
  1202.     "jv" => array("Javanese"),
  1203.     "ka" => array("Georgian"),
  1204.     "kg" => array("Kongo"),
  1205.     "ki" => array("Kikuyu"),
  1206.     "kj" => array("Kwanyama"),
  1207.     "kk" => array("Kazakh", "╥Ü╨░╨╖╨░╥¢"),
  1208.     "kl" => array("Greenlandic"),
  1209.     "km" => array("Cambodian"),
  1210.     "kn" => array("Kannada", "α▓òα▓¿α│ìα▓¿α▓í"),
  1211.     "ko" => array("Korean", "φò£Ω╡¡∞û┤"),
  1212.     "kr" => array("Kanuri"),
  1213.     "ks" => array("Kashmiri"),
  1214.     "ku" => array("Kurdish", "Kurd├«"),
  1215.     "kv" => array("Komi"),
  1216.     "kw" => array("Cornish"),
  1217.     "ky" => array("Kirghiz", "╨Ü╤ï╤Ç╨│╤ï╨╖"),
  1218.     "la" => array("Latin", "Latina"),
  1219.     "lb" => array("Luxembourgish"),
  1220.     "lg" => array("Luganda"),
  1221.     "ln" => array("Lingala"),
  1222.     "lo" => array("Laothian"),
  1223.     "lt" => array("Lithuanian", "Lietuvi┼íkai"),
  1224.     "lv" => array("Latvian", "Latvie┼íu"),
  1225.     "mg" => array("Malagasy"),
  1226.     "mh" => array("Marshallese"),
  1227.     "mi" => array("Maori"),
  1228.     "mk" => array("Macedonian", "╨£╨░╨║╨╡╨┤╨╛╨╜╤ü╨║╨╕"),
  1229.     "ml" => array("Malayalam", "α┤«α┤▓α┤»α┤╛α┤│α┤é"),
  1230.     "mn" => array("Mongolian"),
  1231.     "mo" => array("Moldavian"),
  1232.     "mr" => array("Marathi"),
  1233.     "ms" => array("Malay", "Bahasa Melayu"),
  1234.     "mt" => array("Maltese", "Malti"),
  1235.     "my" => array("Burmese"),
  1236.     "na" => array("Nauru"),
  1237.     "nd" => array("North Ndebele"),
  1238.     "ne" => array("Nepali"),
  1239.     "ng" => array("Ndonga"),
  1240.     "nl" => array("Dutch", "Nederlands"),
  1241.     "no" => array("Norwegian", "Norsk"),
  1242.     "nr" => array("South Ndebele"),
  1243.     "nv" => array("Navajo"),
  1244.     "ny" => array("Chichewa"),
  1245.     "oc" => array("Occitan"),
  1246.     "om" => array("Oromo"),
  1247.     "or" => array("Oriya"),
  1248.     "os" => array("Ossetian"),
  1249.     "pa" => array("Punjabi"),
  1250.     "pi" => array("Pali"),
  1251.     "pl" => array("Polish", "Polski"),
  1252.     "ps" => array("Pashto", "┘╛┌Ü╪¬┘ê"),
  1253.     "pt" => array("Portuguese", "Portugu├¬s"),
  1254.     "qu" => array("Quechua"),
  1255.     "rm" => array("Rhaeto-Romance"),
  1256.     "rn" => array("Kirundi"),
  1257.     "ro" => array("Romanian", "Rom├ón─â"),
  1258.     "ru" => array("Russian", "╨á╤â╤ü╤ü╨║╨╕╨╣"),
  1259.     "rw" => array("Kinyarwanda"),
  1260.     "sa" => array("Sanskrit"),
  1261.     "sc" => array("Sardinian"),
  1262.     "sd" => array("Sindhi"),
  1263.     "se" => array("Northern Sami"),
  1264.     "sg" => array("Sango"),
  1265.     "sh" => array("Serbo-Croatian"),
  1266.     "si" => array("Singhalese"),
  1267.     "sk" => array("Slovak", "Sloven─ìina"),
  1268.     "sl" => array("Slovenian", "Sloven┼í─ìina"),
  1269.     "sm" => array("Samoan"),
  1270.     "sn" => array("Shona"),
  1271.     "so" => array("Somali"),
  1272.     "sq" => array("Albanian", "Shqip"),
  1273.     "sr" => array("Serbian", "╨í╤Ç╨┐╤ü╨║╨╕"),
  1274.     "ss" => array("Siswati"),
  1275.     "st" => array("Sesotho"),
  1276.     "su" => array("Sudanese"),
  1277.     "sv" => array("Swedish", "Svenska"),
  1278.     "sw" => array("Swahili", "Kiswahili"),
  1279.     "ta" => array("Tamil", "α«ñα««α«┐α«┤α»ì"),
  1280.     "te" => array("Telugu", "α░ñα▒åα░▓α▒üα░ùα▒ü"),
  1281.     "tg" => array("Tajik"),
  1282.     "th" => array("Thai", "α╕áα╕▓α╕⌐α╕▓α╣äα╕ùα╕ó"),
  1283.     "ti" => array("Tigrinya"),
  1284.     "tk" => array("Turkmen"),
  1285.     "tl" => array("Tagalog"),
  1286.     "tn" => array("Setswana"),
  1287.     "to" => array("Tonga"),
  1288.     "tr" => array("Turkish", "T├╝rk├ºe"),
  1289.     "ts" => array("Tsonga"),
  1290.     "tt" => array("Tatar", "Tatar├ºa"),
  1291.     "tw" => array("Twi"),
  1292.     "ty" => array("Tahitian"),
  1293.     "ug" => array("Uighur"),
  1294.     "uk" => array("Ukrainian", "╨ú╨║╤Ç╨░╤ù╨╜╤ü╤î╨║╨░"),
  1295.     "ur" => array("Urdu", "╪º╪▒╪»┘ê"),
  1296.     "uz" => array("Uzbek", "o'zbek"),
  1297.     "ve" => array("Venda"),
  1298.     "vi" => array("Vietnamese", "Tiß║┐ng Viß╗çt"),
  1299.     "vo" => array("Volap├╝k"),
  1300.     "wo" => array("Wolof"),
  1301.     "xh" => array("Xhosa", "isiXhosa"),
  1302.     "yi" => array("Yiddish"),
  1303.     "yo" => array("Yoruba", "Yor├╣b├í"),
  1304.     "za" => array("Zhuang"),
  1305.     "zh-hans" => array("Chinese, Simplified", "τ«ÇΣ╜ôΣ╕¡µûç"),
  1306.     "zh-hant" => array("Chinese, Traditional", "τ╣üΘ½öΣ╕¡µûç"),
  1307.     "zu" => array("Zulu", "isiZulu"),
  1308.   );
  1309. }
  1310.  
  1311. ?>
  1312.